package com.bootdo.mall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.bj.service.CategoryService;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.lvyou.domain.LvyouScenicSpot;
import com.bootdo.lvyou.service.LvyouScenicSpotService;
import com.bootdo.mall.domain.GoodSpecs;
import com.bootdo.mall.domain.MallCateGory;
import com.bootdo.mall.service.GoodSpecsService;
import com.bootdo.mall.service.MallCateGoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.mall.domain.MallGood;
import com.bootdo.mall.service.MallGoodService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 商品
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-24 19:42:31
 */
 
@Controller
@RequestMapping("/mall/mallGood")
public class MallGoodController extends BaseController{
	@Autowired
	private LvyouScenicSpotService lvyouScenicSpotService;

	@Autowired
	private MallGoodService mallGoodService;

	@Autowired
	private MallCateGoryService mallCateGoryService;

	@Autowired
	private GoodSpecsService goodSpecsService;
	
	@ModelAttribute
	public MallGood get(@RequestParam(required=false) Long id) {
		if (id != null){
			return mallGoodService.get(id);
		}else{
			return new MallGood();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/mall/mallGood','mall:mallGood:mallGood',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','mall:mallGood:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("mall:mallGood:mallGood")	
	String MallGood(MallGood mallGood,HttpServletRequest request,HttpServletResponse response){
	    return "mall/mallGood/mallGood";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("mall:mallGood:mallGood")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        if(!isAdmin()){
        	query.put("userId",getUserId());
		}
        List<Long> cid  = new ArrayList<>();
        if(null != query.get("categoryId") && StringUtils.isNotEmpty(query.get("categoryId").toString())){
			getChildList(Long.parseLong(query.get("categoryId").toString()),cid);
			query.remove("categoryId");
			query.put("categoryIdList",cid);
        }
		List<MallGood> mallGoodList = mallGoodService.list(query);
		int total = mallGoodService.count(query);
		PageUtils pageUtils = new PageUtils(mallGoodList, total);
		return pageUtils;
	}


	 void  getChildList(Long id,List<Long> cid){
		Map query = new HashMap();
		query.put("parentId",id);
		cid.add(id);
		List<MallCateGory> cateGories = mallCateGoryService.list(query);
		if(cateGories.size() > 0){
			cateGories.stream().forEach(item ->{
				getChildList(item.getId().longValue(),cid);
			});
		}
	}


	@GetMapping("/add")
	@RequiresPermissions("mall:mallGood:edit")
	String add(MallGood mallGood,HttpServletRequest request,HttpServletResponse response){
	    return "mall/mallGood/mallGoodedit";
	}

	/**
	 *  /mall/mallGood/addStock
	 * @param mallGood
	 * @return
	 */
	@PostMapping("/addStock")
	@ResponseBody
	R addStock(MallGood mallGood) {
		 int r = mallGoodService.updateStockAdd(mallGood.getStock(), mallGood.getId());
		 if(r > 0){
		 	return  R.ok("操作成功");
		 }else{
			 return R.error("操作失败");
		 }
	}

	/**
	 *  /mall/mallGood/delStock
	 * @param mallGood
	 * @return
	 */
	@PostMapping("/delStock")
	@ResponseBody
	R delStock(MallGood mallGood) {
		int r = mallGoodService.updateStockDel(mallGood.getStock(), mallGood.getId());
		if(r > 0){
			return  R.ok("操作成功");
		}else{
			return R.error("操作失败");
		}
	}



	@PostMapping("/getById")
	@ResponseBody
	R getById(MallGood mallGood) {
		if(null != mallGood.getCategoryId()){
			MallCateGory mallCateGory  = mallCateGoryService.get(mallGood.getCategoryId().intValue());
			if(null != mallCateGory){
				mallGood.setCategoryName(mallCateGory.getName());
			}
		}
		return R.ok().put("i",mallGood);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallGood:edit")
	public R save( MallGood mallGood){
		if(!isAdmin()){
			Map map = new HashMap();
			map.put("userId",getUserId());
			if(null == mallGood.getUserId()){
				mallGood.setUserId(getUserId());
				mallGood.setMerchantId(getUserId());
				mallGood.setMerchantName(getUser().getMerchantName());
				mallGood.setUserName(getUser().getMerchantName());
			}
		}
		if(StringUtils.isNotEmpty(mallGood.getContext())){
			Document document =  Jsoup.parse(mallGood.getContext());
			List<Element> elements =  document.getElementsByTag("img");
			for(Element element : elements){
				element.attr("style","max-width:100%;");
			}
			mallGood.setContext(document.toString());
		}
		if(mallGoodService.save(mallGood)>0){
			return R.ok();
		}
		return R.error();
	}


	/**
	 * 上架
	 */
	@ResponseBody
	@PostMapping("/shangjia")
	@RequiresPermissions("mall:mallGood:edit")
	public R shangjia( MallGood mallGood){
		MallGood mallGood1 =  mallGoodService.get(mallGood.getId());
		mallGood1.setIsShow(mallGood.getIsShow());
		if(mallGoodService.save(mallGood1)>0){
			return R.ok();
		}
		return R.error();
	}


	/**
	 * 修改返回积分
	 */
	@ResponseBody
	@PostMapping("/updateJifen")
	@RequiresPermissions("mall:mallGood:edit")
	public R updateJifen(@RequestParam("id")Long id,@RequestParam("returnIntegral") Double returnIntegral){
		GoodSpecs goodSpecs =  goodSpecsService.get(id);
		goodSpecs.setReturnIntegral(returnIntegral);
		if(goodSpecsService.save(goodSpecs)>0){
			return R.ok();
		}
		return R.error();
	}



	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:mallGood:edit")
	public R remove( Long id){
		if(mallGoodService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:mallGood:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		mallGoodService.batchRemove(ids);
		return R.ok();
	}
	
}